package com.jackie.coupon.template.service.impl;

import com.jackie.coupon.template.api.beans.CouponTemplateInfo;
import com.jackie.coupon.template.api.enums.CouponType;
import com.jackie.coupon.template.converter.CouponTemplateConverter;
import com.jackie.coupon.template.dao.CouponTemplateDao;
import com.jackie.coupon.template.dao.entiry.CouponTemplate;
import com.jackie.coupon.template.service.CouponTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;

/**
 * CouponTemplateServiceImpl
 *
 * @author Jackie Hou
 * @date 2023/11/17 08:52
 * @version 1.0
 */
@Slf4j
@Service
public class CouponTemplateServiceImpl implements CouponTemplateService {

    @Override
    public CouponTemplateInfo createTemplate(CouponTemplateInfo request) {
        if (request.getShopId() != null) {
            Integer count = couponTemplateDao.countByShopIdAndAvailable(request.getShopId(), true);
            if (count >= 100) {
                log.error("coupon template count is overflow! count = {}", count);
                throw new UnsupportedOperationException("coupon template count is overflow!");
            }
        }

        CouponTemplate template = CouponTemplate.builder()
                .name(request.getName())
                .description(request.getDesc())
                .category(CouponType.convert(request.getType()))
                .available(true)
                .shopId(request.getShopId())
                .rule(request.getRule())
                .build();

       template =  couponTemplateDao.save(template);
       return CouponTemplateConverter.convert2TemplateInfo(template);
    }

    @Override
    public CouponTemplateInfo loadTemplateInfo(Long id) {
        return null;
    }

    @Override
    public CouponTemplateInfo cloneTemplate(Long templateId) {
        return null;
    }

    @Override
    public void deleteTemplate(Long id) {

    }

    @Override
    public Map<Long, CouponTemplateInfo> getTemplateInfoMap(Collection<Long> ids) {
        return null;
    }

    @Autowired
    private CouponTemplateDao couponTemplateDao;
}
